/**
 * Created by zhengjiachao on 2017/6/21.
 */
(function() {
    ADF.actionModel.XYDirBaseModel = function() {
        ADF.actionModel.XYDirBaseModel.superclass.constructor.call(this);
    };

    COM.util.defineClass(ADF.actionModel.XYDirBaseModel, AGEF.ActionModel, {
        execute: function(graphicalViewer, dir){
            if (graphicalViewer.getSelection()) {
                var commands = new COM.gef.CompoundCommand();
                graphicalViewer.getSelection().forEach(function (selectionItem) {
                    var model = selectionItem.model;
                    if (model) {
                        var bounds = selectionItem.model.getLayoutData();
                        if(bounds instanceof COM.draw2d.Rectangle){
                            var cmd = COM.gef.Command.createCommand({
                                execute: function execute() {
                                    if (this.dir == "left") {
                                        bounds.x = bounds.x - 2;
                                    } else if (this.dir == "up") {
                                        bounds.y = bounds.y - 2;
                                    } else if (this.dir == "right") {
                                        bounds.x = bounds.x + 2;
                                    } else if (this.dir == "down") {
                                        bounds.y = bounds.y + 2;
                                    }

                                    model.setLayoutData(new COM.draw2d.Rectangle(bounds.x, bounds.y, bounds.width, bounds.height));
                                },
                                undo: function undo() {
                                    if (this.dir == "left") {
                                        bounds.x = bounds.x + 2;
                                    } else if (this.dir == "up") {
                                        bounds.y = bounds.y + 2;
                                    } else if (this.dir == "right") {
                                        bounds.x = bounds.x - 2;
                                    } else if (this.dir == "down") {
                                        bounds.y = bounds.y - 2;
                                    }

                                    model.setLayoutData(new COM.draw2d.Rectangle(bounds.x, bounds.y, bounds.width, bounds.height));
                                }
                            });
                            cmd.dir = dir;
                            cmd.model = model;
                            commands.add(cmd);
                        }
                    }
                });
                graphicalViewer.getEditDomain().getCommandStack().execute(commands);
            }
        },
        getEnabledState : function(graphicalViewer){
            var enable = true;
            if (graphicalViewer.getSelection()) {
                graphicalViewer.getSelection().forEach(function (selectionItem) {
                    var model = selectionItem.model;
                    if (model) {
                        var bounds = selectionItem.model.getLayoutData();
                        if(!(bounds instanceof COM.draw2d.Rectangle)){
                            enable = false;
                            return;
                        }
                    }
                });
            }
            return enable;
        }
    });
})();
